package com.imay.live.fragment.discovery;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.imay.live.R;
import com.imay.live.activity.discovery.CelebrityActivity;
import com.imay.live.adapter.CelebrityLinearAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.model.CelebrityModel;
import com.imay.live.model.CelebrityTitleModel;
import com.imay.live.model.FeedModel;
import com.imay.live.model.HotUsersDataJsonModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.custom.PastseRelatvieLayout2;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by liangzhenxiong on 2017/4/7 0006.
 * 发现-名人
 */

public class CelebrityFragment extends LoadFragment {

    PastseRelatvieLayout2 pastseView;
    private boolean loadmore = true;

    public static CelebrityFragment newInstance() {
        return new CelebrityFragment();
    }

    @Override
    public void onResume() {
        super.onResume();
        mRefreshLayout.setEnableLoadmore(loadmore);
    }

    public void updateFeedSuggest(ArrayList<FeedModel> feedSuggestModel) {
        super.updateFeedSuggest(feedSuggestModel);
        if (pastseView != null) {
            //pastseView.setModel(feedSuggestModel);
        }
    }

    public void updateLabels(ArrayList<String> hotLabels) {
        this.hotLabels = hotLabels;
        if (pastseView != null) {
            pastseView.setModel(hotLabels);
        }
    }

    @Override
    public void setTabH(int tabH) {
        super.setTabH(tabH);
        addFooterView(tabH);
    }

    @Override
    public boolean getsetEnableRefresh() {
        if (mLinearRecyclerView != null) {
            RecyclerView.LayoutManager layoutManager = mLinearRecyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                int index = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
                return index == 0;
            }
        }
        return super.getsetEnableRefresh();
    }

    @Override
    public void onLoadMore() {
        mRefreshLayout.finishLoadmore();
        loadmore = false;
        mRefreshLayout.setEnableLoadmore(loadmore);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        try {
            Result<HotUsersDataJsonModel> result = JSON.parseObject(data, new TypeReference<Result<HotUsersDataJsonModel>>() {
            }.getType());
            if (result.result == CodeSuccess && result.data != null) {
                completeLoad();
                loadmore = false;
                mRefreshLayout.setEnableLoadmore(loadmore);
                mdatas.clear();
                HotUsersDataJsonModel hotUsersDataJsonModel = result.data;
                ArrayList<MultiItemEntity> datastemp = new ArrayList<MultiItemEntity>();
                if (hotUsersDataJsonModel.getRed() != null && hotUsersDataJsonModel.getRed().length > 0) {
                    CelebrityTitleModel celebrityTitleModel = new CelebrityTitleModel();
                    CelebrityModel celebrityModel = new CelebrityModel();
                    celebrityModel.setUserModels(hotUsersDataJsonModel.getRed());
                    celebrityTitleModel.setTitle(getString(R.string.str_imay_title));
                    celebrityTitleModel.setHottype("red");
                    datastemp.add(celebrityTitleModel);
                    datastemp.add(celebrityModel);
                }
                if (hotUsersDataJsonModel.getArt() != null && hotUsersDataJsonModel.getArt().length > 0) {
                    CelebrityTitleModel celebrityTitleModel = new CelebrityTitleModel();
                    CelebrityModel celebrityModel = new CelebrityModel();
                    celebrityModel.setUserModels(hotUsersDataJsonModel.getArt());
                    celebrityTitleModel.setTitle(getString(R.string.str_imay_title_star));
                    celebrityTitleModel.setHottype("art");
                    datastemp.add(celebrityTitleModel);
                    datastemp.add(celebrityModel);
                }
                if (hotUsersDataJsonModel.getNewt() != null && hotUsersDataJsonModel.getNewt().length > 0) {
                    CelebrityTitleModel celebrityTitleModel = new CelebrityTitleModel();
                    CelebrityModel celebrityModel = new CelebrityModel();
                    celebrityModel.setUserModels(hotUsersDataJsonModel.getNewt());
                    celebrityTitleModel.setTitle(getString(R.string.str_imay_title_newstar));
                    celebrityTitleModel.setHottype("new");
                    datastemp.add(celebrityTitleModel);
                    datastemp.add(celebrityModel);
                }
                mdatas.addAll(datastemp);
                if (mdatas.size() > 0) {
                    addFooterView(tabH);
                }
                mAdapter.notifyDataSetChanged();
            } else {
                loadError(getResources().getString(R.string.str_loaderror));
            }

        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        loadError(getResources().getString(R.string.str_loaderror));
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        ProtoRequestHelper.task(ProtoHttpFactory.getHotImay()).setTag(this.hashCode()).request(this);
    }

    @Override
    void initAdapte() {
        super.initAdapte();
        mAdapter = new CelebrityLinearAdapter(mdatas);
        mLinearRecyclerView.setAdapter(mAdapter);
        mLinearRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.rl_cele) {
                    CelebrityTitleModel c = (CelebrityTitleModel) adapter.getItem(position);
                    if (c == null) {
                        return;
                    }
                    CelebrityActivity.start(getActivity(), c.getHottype(), c.getTitle());
                }
            }
        });
    }

    @Override
    void oncreate() {
        super.oncreate();
        //pastseView = new PastseRelatvieLayout2(getActivity());
        //pastseView.setModel(hotLabels);
        //pastseView.setShowPagging();
        //mAdapter.addHeaderView(pastseView);
        addFooterView(tabH);
        MobclickAgent.onEvent(getActivity(), "find_famous");
    }

}
